package com.lili.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lili.model.promotion.Coupon;
import com.lili.model.order.entity.order.OrderDetail;
import com.lili.model.order.entity.order.Order;
import com.lili.model.order.entity.sqlException;
import com.lili.order.mapper.order.OrderMapper;
import com.lili.order.mapper.order.OrderDetailMapper;
import com.lili.order.service.order.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.Month;

@Service
public class OrderDetailServiceImpl implements OrderDetailService {

    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderMapper orderMapper;
    /**
     *   增加
     */
     @Transactional
    public  int save(OrderDetail orderDetail){
        String sn=createSn();
        orderDetail.setSn(sn);
        double storePrice=0;
        double platePrice=0;
         double discountPrice=0;
        //根据useStoreMemberCouponIds店铺会员优惠券查询
         Coupon StoreCoupon=null;

         if(StoreCoupon!=null){
             storePrice=StoreCoupon.getPrice();
         }
         //使用的平台会员优惠券id  usePlatformMemberCouponId
         Coupon PlatCoupon=null;

         if(PlatCoupon!=null){
             platePrice=PlatCoupon.getPrice();
         }

         double totalPrice=orderDetail.getFlowPrice();

         discountPrice=totalPrice-storePrice-platePrice;
         orderDetail.setDiscountPrice(discountPrice);
         if(orderDetail.getDistributionId()!=""){
                     //保存分销信息
                 }
            //新建立一个有关分销的信息表，我们根据分销员id查询分销信息，在保存订单的时候保存分销
        //保存订单
          Order order =copyOrder(orderDetail);
          int rs=orderMapper.insert(order);
         if(rs!=1){
            return rs;
         }
         rs=orderDetailMapper.insert(orderDetail);
         if(rs!=1) {
             putThrow("订单保存失败！");
         }
         return rs;
    }
    /**
     *   删除
     */
    @Transactional
    @Override
    public int deleteBySn(String sn) {
        int rs= orderDetailMapper.deleteBySn(sn);
        if(rs==0){
            return rs;
        }
        rs= orderMapper.deleteBySn(sn);
        if(rs==0){
            putThrow("删除订单明细失败");
        }
        return rs;
    }
    @Transactional
    @Override
    public int deleteForever(String sn) {
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("sn", sn);
        int rs= orderDetailMapper.delete(wrapper);
        if(rs==0){
            return rs;
        }
        QueryWrapper<Order> wrapperOrder = new QueryWrapper<>();
        wrapper.eq("sn", sn);
        rs= orderMapper.delete(wrapperOrder);
        if(rs==0){
            putThrow("删除订单明细失败");
        }
        return rs;
    }
    /**
     *   更改
     */
    @Transactional
    @Override
    public int updateBySn(OrderDetail orderDetail) {
        String sn= orderDetail.getSn();
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("sn", sn); // 添加相等条件
        int rs= orderDetailMapper.update(orderDetail,wrapper);
        if(rs==0){
            return rs;
        }
        if(orderDetail.getOrderStatus()!=null&&orderDetail.getOrderStatus()!=""){  //只有更改状态的时候才更改order，其他情况只更新明细
            Order order =new Order();
            order.setOrderStatus(orderDetail.getOrderStatus());
            QueryWrapper<Order> Corewrapper = new QueryWrapper<>();
            Corewrapper.eq("sn", sn); // 添加相等条件
            rs= orderMapper.update(order,Corewrapper);
            if(rs==0){
                putThrow("订单状态更改失败");
            }
        }
        return rs;
    }

    /**
     *   查询
     */
    @Override
    public OrderDetail getOrderBySn(String sn) {
        QueryWrapper<OrderDetail> wrapper=new QueryWrapper<>();
        wrapper.eq("sn",sn);
        return orderDetailMapper.selectOne(wrapper);
    }



    protected void putThrow(String message) {
        try {
            throw new sqlException(message);
        } catch (sqlException e) {
            throw new RuntimeException(e);
        }
    }
    public String createSn(){
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("当前日期和时间: " + currentDateTime);
        // 获取年、月、日
        int year = currentDateTime.getYear();
        Month currentMonth = Month.JULY;
         String userId="11223344556677";
        int month = currentMonth.getValue();
        int day = currentDateTime.getDayOfMonth();
        int hour = currentDateTime.getHour();
        int minute = currentDateTime.getMinute();
        int second=currentDateTime.getSecond();
        return  "O"+year+formatTime(month)+formatTime(day)+formatTime(hour)+formatTime(minute)+formatTime(second)+userId;
    }
    public String formatTime(int time )
    {
        return String.format("%02d",time);
    }
    public Order copyOrder(OrderDetail orderDetail){
        Order order=new Order();
        order.setSn(orderDetail.getSn());
        order.setOrderType(orderDetail.getOrderType());
        order.setOrderStatus(orderDetail.getOrderStatus());
        order.setClientType(orderDetail.getClientType());
        order.setFlowPrice(orderDetail.getFlowPrice());
        order.setCreateTime(orderDetail.getCompleteTime());
        order.setMemberName(orderDetail.getMemberName());
        order.setIsEntity(orderDetail.getIsEntity());
        order.setStoreId(orderDetail.getStoreId());
        return  order;
    }

}
